Skip to content

Method: FieldDescriptor(URI, Field)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.model.descriptors;
16:
17: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
18: import cz.cvut.kbss.jopa.utils.ErrorUtils;
19:
20: import java.lang.reflect.Field;
21: import java.net.URI;
22: import java.util.Collection;
23: import java.util.Collections;
24: import java.util.Objects;
25: import java.util.Set;
26:
27: /**
28: * Describes a singular data property or a plural data, object or annotation property field.
29: *
30: * @author ledvima1
31: */
32: public class FieldDescriptor extends Descriptor {
33:
34: private final Field field;
35:
36: public FieldDescriptor(Field attribute) {
37: super();
38: Objects.requireNonNull(attribute, ErrorUtils.constructNPXMessage("attribute"));
39: this.field = attribute;
40: }
41:
42: public FieldDescriptor(URI context, Field attribute) {
43: super(context);
44: Objects.requireNonNull(attribute, ErrorUtils.constructNPXMessage("attribute"));
45: this.field = attribute;
46: }
47:
48: @Override
49: public Collection<Descriptor> getAttributeDescriptors() {
50: return Collections.<Descriptor>singleton(this);
51: }
52:
53: @Override
54: public Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute) {
55: return getFieldDescriptor(attribute.getJavaField());
56: }
57:
58: @Override
59: public void addAttributeDescriptor(Field attribute, Descriptor descriptor) {
60: // Do nothing
61: }
62:
63: @Override
64: public void addAttributeContext(Field attribute, URI context) {
65: // Do nothing
66: }
67:
68: private Descriptor getFieldDescriptor(Field field) {
69: if (this.field.equals(field)) {
70: return this;
71: }
72: throw new IllegalArgumentException("This field descriptor does not describe field " + field);
73: }
74:
75: @Override
76: protected Set<URI> getContextsInternal(Set<URI> contexts, Set<Descriptor> visited) {
77: if (context == null) {
78: return null;
79: }
80: contexts.add(context);
81: visited.add(this);
82: return contexts;
83: }
84:
85: Field getField() {
86: return field;
87: }
88:
89: @Override
90: public int hashCode() {
91: final int prime = 31;
92: int result = super.hashCode();
93: result = prime * result + field.hashCode();
94: return result;
95: }
96:
97: @Override
98: public boolean equals(Object obj) {
99: if (this == obj)
100: return true;
101: if (!super.equals(obj))
102: return false;
103: if (getClass() != obj.getClass())
104: return false;
105: FieldDescriptor other = (FieldDescriptor) obj;
106: if (!field.equals(other.field))
107: return false;
108: return true;
109: }
110: }